﻿var id;
$(function () {
    $("#txtDate").datepicker();
    $('#ckeditor1').ckeditor();
    id = querySt('id');

    if (id != "") {
        var data = '{"AnnouncementID": "' + id + '"}';
        ajaxCall('../DataService.asmx/GetAnnouncementByID', false, data,
                    function (msg) {
                        $('#txtTitle').val(msg.d.Title);
                        $('#txtDate').val(JSONDateToNormal(msg.d.Date));
                        $('#ckeditor1').val(msg.d.Description);
                    },
                    function () {
                        alert(msg.toString("Error happend while performing this action"));
                    }
                );
    }

});


function InsertUpdateAnnouncement() {
    $.validator.addMethod("dateRegex", function (value, element) {
        return this.optional(element) || /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/i.test(value);
    }, "Example date format: mm/dd/yyyy");

    $("#formValidateAnnouncement").validate({
        rules: {
            title: "required",
            date: {
                required: true,
                dateRegex: true
            },
            editor1: "required"

        },
        messages: {
            title: "Title is required",
            date: {
                required: "Date is required",
                dateRegex: "Wrong date format. Please choose date from the calendar"

            },
            editor1: "Description is required"

        }
    });

    CKEDITOR.instances["ckeditor1"].updateElement();
    if ($("#formValidateAnnouncement").valid()) {
        var data;
        if (id == "") {
            data = "{'title': '" + $("#txtTitle").val() + "', 'date': '" + $("#txtDate").val() + "' , 'description' : '" + CKEDITOR.instances["ckeditor1"].getData() + "'}";
            ajaxCall('../DataService.asmx/InsertAnnouncement', false, data,
                        function (msg) {
                            alert("Inserted successfully");
                            $('#txtTitle').val("");
                            $('#txtDate').val("");
                            $('#ckeditor1').val("");
                        },
                        function () {
                            alert("Error happend while performing this action");
                        }
                      );
        }
        else {
            data = "{'id': '" + id + "','title': '" + $("#txtTitle").val() + "', 'date': '" + $("#txtDate").val() + "' , 'description' : '" + CKEDITOR.instances["ckeditor1"].getData() + "'}";
            ajaxCall('../DataService.asmx/UpdateAnnouncement', false, data,
                        function (msg) {
                            alert("Updated successfully");
                            window.location = "ManageAnnouncements.aspx";
                        },
                        function () {
                            alert("Error happend while performing this action");
                        }
                    );

        }
    }
}